// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from '@/store'
import bus from '@/utils/bus'
import apis from '@/apis'
import 'animate.css'
import Mint from 'mint-ui';
import 'mint-ui/lib/style.css'
import VueCookie from 'vue-cookie'
// import VueAsset from 'asset-web-support'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


Vue.use(ElementUI);

import vmaAssist from 'vma-vue-assist'
import config from '@/config'
Vue.use(Mint);
Vue.use(vmaAssist, {
    plugins: {
        axios: {
            // 将extend到axios.defaults
            defaults: {
                baseURL: config.host,
                responseType: 'text',
                headers: {
                    session_id: decodeURIComponent(VueCookie.get('session_id') || '')
                        // session_id: function() {
                        //     let sessionId = VueCookie.get('session_id') || ''
                        //     return decodeURIComponent(sessionId)
                        // }
                }
            },
            // 所有的拦截器只要配置为false则不启用
            interceptor: {
                // 全局错误拦截器
                errorHandle: {
                    handleError(error) {
                        console.log(error)
                        Vue.$toast({
                            message: error.message,
                            duration: 2000,
                        })
                    }
                },
                // http请求显示加载中
                loading: {
                    // 第一个http请求发起时
                    showLoader() {
                        console.log('aaa')
                        Vue.$indicator.open()
                    },
                    // 最后一个http请求结束时
                    hideLoader() {
                        Vue.$indicator.close()
                    }
                },
                removeEmpty: true,
                returnResponseData: true
            },
            transformResponse: {
                // response.data下划线转驼峰
                hump: true
            }
        }
    }
})


Object.defineProperties(Vue.prototype, {
    $apis: {
        value: apis
    },
    $bus: {
        value: bus
    }
})
Vue.config.productionTip = false

const HISTORY_CACHE = Object.create(null)
let HISTORY_CACHE_KEY_COUNT = 0
HISTORY_CACHE['/'] = 0
router.beforeEach((to, from, next) => {
    console.log(to, from)
        /*页面级animated--begin*/
    let toName = to.name || to.path
    let fromName = from.name || from.path
    const toIndex = HISTORY_CACHE[toName]
    const fromIndex = HISTORY_CACHE[fromName]
    store.dispatch('toName', toName)
    store.dispatch('fromName', fromName)
        // 判断当前页面是否进入过 是：如果当前权级>前一个权级，进入（forward），否则退出（reverse）
        // 否：记录当前路径-权级+1，并进入（forward）
    if (toIndex) {
        if (parseInt(toIndex, 10) > parseInt(fromIndex, 10)) {
            store.dispatch('direction', 'forward')
        } else {
            store.dispatch('direction', 'reverse')
        }
    } else {
        ++HISTORY_CACHE_KEY_COUNT
        toName !== '/' && (HISTORY_CACHE[toName] = HISTORY_CACHE_KEY_COUNT)
        store.dispatch('direction', 'forward')
    }
    /*页面级animated--end*/
    next()
})
new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})